package com.jjz.id.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * @author ZJL
 * @create 2025-05-27-15:44
 * 用于配置 MyBatis Plus 的核心拦截器
 */

@Configuration(
        proxyBeanMethods = false
)
@EnableTransactionManagement(
        order = 100
)
public class MybatisAutoConfiguration {
    private static final Logger log = LoggerFactory.getLogger(MybatisAutoConfiguration.class);

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());  //乐观锁：配合 @Version 字段使用，防止并发写入数据冲突
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());  //	分页插件：支持分页查询（如 Page<T>）
        interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());  //	防全表更新删除：拦截没有 WHERE 条件的 update 或 delete 防止误操作
        return interceptor;
    }
}